﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZH.Infrastructure.Attr;
using ZH.Infrastructure.Dto.Request.Tech;
using ZH.Infrastructure.Dto.Response;
using ZH.Infrastructure.Dto.Response.Dictionary;
using ZH.Infrastructure.Dto.Response.Tech;
using ZH.Infrastructure.EFCore.MySql;
using ZH.Infrastructure.IOC;
using ZH.Infrastructure.Tools;
using ZH.Service.Interface;

namespace ZH.Service.Instance
{
    [Inject_]
    internal class Tech_Service : Base_Service, I_Tech_Service
    {
        public readonly Tech_IOC _tech_IOC;
        public readonly IConfiguration _configuration;

        public Tech_Service(
            Tech_IOC tech_IOC,
            IConfiguration configuration
            )
        {
            _tech_IOC = tech_IOC;
            _configuration = configuration;
        }

        public async Task<Api_Response_Dto> Add_Tech(Tech_Request_Dto dto)
        {
            Technique te = new Technique();
            te.TechId = Config.GUID2();
            te.TechName = dto.Tech_name;
            te.TechPrice = dto.tech_price;
            te.TechType = dto.tech_type;
            te.TechDesc = dto.tech_desc;
            te.TechDuration = dto.tech_duration;
            te.IsBan = 1;
            te.IsSafe = dto.is_safe.ToString();

            _tech_IOC._tech_Service.Add(te);
            var code = await _tech_IOC._tech_Service.SaveChangesAsync();

            return Result(code, "添加成功");



        }
        /// <summary>
        /// 删除技法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Response_Dto> Delete_Tech(string id)
        {
            var data = await _tech_IOC._tech_Service.QueryAll(d => d.TechId == id).FirstOrDefaultAsync();
            if (data == null)
            {
                return Result(0, "未找到该技法");
            }
            _tech_IOC._tech_Service.Delete(data);
            int code = _tech_IOC._tech_Service.SaveChanges();
            return Result(code, "删除成功");
        }

        /// <summary>
        /// 启用禁用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> DS_Tech(string id)
        {
            var data = await _tech_IOC._tech_Service.QueryAll(d => d.TechId == id).FirstOrDefaultAsync();
            if (data == null)
            {
                return Result(1, "没找到该技法");
            }
            data.IsBan = data.IsBan == 1 ? 0 : 1;
            _tech_IOC._tech_Service.Update(data);
            var code = _tech_IOC._tech_Service.SaveChanges();
            return Result(code, "修改成功");
        }

        /// <summary>
        /// 获取技法列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Get_Tech(Tech_Response_Dto dto)
        {
            var data = await _tech_IOC._tech_Service.QueryAll(out int total,
                  Convert.ToInt32(dto.page), Convert.ToInt32(dto.limit),
                  true,
                  o => o.TechId,
                    d => (string.IsNullOrEmpty(dto.key) || d.TechName.Contains(dto.key))
                  ).Select(d => new Tech_Response_Dto
                  {
                      tid = d.TechId,
                      tname = d.TechName,
                      type = d.TechType,
                      price = (decimal)d.TechPrice,
                      isBan = Convert.ToInt32(d.IsBan),
                      tdesc = d.TechDesc,
                      techduration = d.TechDuration.ToString(),
                      is_safe = d.IsSafe,

                  }).ToListAsync();
            var type = _configuration["Dictionary_Type:Tech"];
            var dic = _tech_IOC._dictionary_EFCore.QueryAll(d => d.DparentId == type).ToList();
            var query = from d in data
                        join c in dic on d.type equals c.Did
                        select new Tech_Response_Dto
                        {
                            tid = d.tid,
                            tname = d.tname,
                            type = d.type,
                            Tyname = c.Dname,
                            isBan = d.isBan,
                            tdesc = d.tdesc,
                            price = d.price,
                            techduration = d.techduration,
                            is_safe = d.is_safe,
                        };
            return Result(1, "ok", new
            {
                total,
                query
            });

        }


        /// <summary>
        /// 获取技法列表(无分页)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Get_Cashier_Tech()
        {
            var data = await _tech_IOC._tech_Service.QueryAll().Select(d => new Tech_Response_Dto
                  {
                      tid = d.TechId,
                      tname = d.TechName,
                      type = d.TechType,
                      price = (decimal)d.TechPrice,
                      isBan = Convert.ToInt32(d.IsBan),
                      tdesc = d.TechDesc,
                      techduration = d.TechDuration!.Value.ToString(),
                      is_safe = d.IsSafe,

                  }).ToListAsync();
            var total = data.Count();
            var type = _configuration["Dictionary_Type:Tech"];
            var dic = _tech_IOC._dictionary_EFCore.QueryAll(d => d.DparentId == type).ToList();
            var query = from d in data
                        join c in dic on d.type equals c.Did
                        select new Tech_Response_Dto
                        {
                            tid = d.tid,
                            tname = d.tname,
                            type = d.type,
                            Tyname = c.Dname,
                            isBan = d.isBan,
                            tdesc = d.tdesc,
                            price = d.price,
                            techduration = d.techduration,
                            is_safe = d.is_safe,
                        };
            return Result(1, "ok", new
            {
                total,
                query
            });

        }

        /// <summary>
        /// 获取技法类别
        /// </summary>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Get_Tech_Types()
        {
            var type = _configuration["Dictionary_Type:Tech"];
            if (string.IsNullOrEmpty(type))
            {
                return Result(0, "参数有误");
            }
            var data = await _tech_IOC._dictionary_EFCore.QueryAll(d => d.DparentId == type && d.DisBan == 0).Select(d => new
            Dictionary_Response_Dto
            {
                id = d.Did,
                name = d.Dname,
                isBan = d.DisBan,
                parent = d.DparentId
            }).ToListAsync();
            return Result(1, "ok", data);
        }

        /// <summary>
        /// 修改技法
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Response_Dto> Update_Tech(Tech_Request_Dto dto)
        {
            var data = await _tech_IOC._tech_Service.QueryAll(d => d.TechId == dto.Tech_id).FirstOrDefaultAsync();
            data.TechDuration = dto.tech_duration;
            data.TechName = string.IsNullOrEmpty(dto.Tech_name) ? data.TechName : dto.Tech_name;
            data.TechPrice = dto.tech_price;
            data.TechType = dto.tech_type;
            data.IsSafe = data.IsSafe;
            data.TechDesc = dto.tech_desc;
            _tech_IOC._tech_Service.Update(data);
            var Code = _tech_IOC._tech_Service.SaveChanges();
            return Result(Code, "修改成功");
        }
    }
}
